package com.joyoung.common.api;

import android.support.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings({"WeakerAccess", "unused"})
public class ApiConfig {

    private Builder mBuilder;

    private static ApiConfig sInstance;
    public static final String URL_KEY_HEADER = "Url-Key";

    public static ApiConfig getInstance() {
        return sInstance;
    }

    public static void init(@NonNull Builder builder) {
        sInstance = new ApiConfig(builder);
    }

    private ApiConfig(Builder builder) {
        if (builder != null) {
            this.mBuilder = builder;
        } else {
            this.mBuilder = new Builder();
        }
    }

    public int getConnectTimeout() {
        return mBuilder.mConnectTimeout;
    }

    public int getReadTimeout() {
        return mBuilder.mReadTimeout;
    }

    public int getWriteTimeout() {
        return mBuilder.mWriteTimeout;
    }

    public boolean isRetryOnConnectionFailure() {
        return mBuilder.mRetryOnConnectionFailure;
    }

    public String getBaseUrl() {
        return mBuilder.mBaseUrl;
    }

    public Map<String, String> getUrlMap() {
        return mBuilder.mUrlMap;
    }

    public String getAppId() {
        return mBuilder.mAppId;
    }

    public String getAppSecret() {
        return mBuilder.mAppSecret;
    }

    public static class Builder {
        private static final int TIMEOUT_CONNECT = 20;
        private static final int TIMEOUT_READ = 20;
        private static final int TIMEOUT_WRITE = 10;

        // 超时时间，单位秒
        private int mConnectTimeout;
        private int mReadTimeout;
        private int mWriteTimeout;
        /**
         * 连接失败后是否重连
         */
        private boolean mRetryOnConnectionFailure;
        /**
         * base url，api接口地址必须以"/"结尾
         */
        private String mBaseUrl;
        /**
         * 多个BaseUrl使用这个map承接，通过 {@link #addUrl}方法添加不同的url
         */
        private Map<String, String> mUrlMap;
        private String mAppId;
        private String mAppSecret;

        public Builder() {
            this.mConnectTimeout = TIMEOUT_CONNECT;
            this.mReadTimeout = TIMEOUT_READ;
            this.mWriteTimeout = TIMEOUT_WRITE;
            this.mRetryOnConnectionFailure = true;
            this.mBaseUrl = null;
        }

        public Builder connectTimeout(int connectTimeout) {
            this.mConnectTimeout = connectTimeout;
            return this;
        }

        public Builder readTimeout(int readTimeout) {
            this.mReadTimeout = readTimeout;
            return this;
        }

        public Builder writeTimeout(int writeTimeout) {
            this.mWriteTimeout = writeTimeout;
            return this;
        }

        public Builder retryOnConnectionFailure(boolean retryOnConnectionFailure) {
            this.mRetryOnConnectionFailure = retryOnConnectionFailure;
            return this;
        }

        public Builder baseUrl(String baseUrl) {
            this.mBaseUrl = baseUrl;
            return this;
        }

        /**
         * 添加不同的BaseUrl，注解添加key
         *
         * @param key 对应url的key，用来寻找相应的url
         * @param url 需要添加url
         */
        public Builder addUrl(String key, String url) {
            if (mUrlMap == null) {
                mUrlMap = new HashMap<>();
            }
            mUrlMap.put(key, url);
            return this;
        }

        public Builder appId(String appId) {
            this.mAppId = appId;
            return this;
        }

        public Builder appSecret(String appSecret) {
            this.mAppSecret = appSecret;
            return this;
        }

        public ApiConfig build() {
            return new ApiConfig(this);
        }

    }
}
